package edu.maping.easy.painter.plugins;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import controlP5.ControlP5;

import codeanticode.glgraphics.GLGraphicsOffScreen;
import deadpixel.keystone.CornerPinSurface;

import processing.core.PApplet;
import processing.core.PImage;
import processing.core.PVector;
import edu.maping.easy.ControlFacade;
import edu.maping.easy.gui.ButtonList;
import edu.maping.easy.gui.GUIEvent;
import edu.maping.easy.gui.GuiFactory;
import edu.maping.easy.model.Point;
import edu.maping.easy.model.PolygonThing;
import edu.maping.easy.model.Thing;
import edu.maping.easy.model.World;
import edu.maping.easy.painter.EventPainter;
import edu.maping.easy.painter.ThingPainterDelegate;
import edu.maping.easy.painter.UtilPainter;
import edu.maping.easy.painter.cornerpin.CornerPin;
import edu.maping.easy.sequence.ActionConf;
import edu.maping.easy.sequence.ticker.TickerListener;

public class FotoViewer implements ThingPainterDelegate, TickerListener,
		CornerPin, TimeRunner {

	private String photoPath = null;

	private UtilPainter utilPainter = new UtilPainter();

	private PApplet pApplet;
	private PImage currentPhoto = null;
	private PImage nextPhoto = null;
	private File[] files;
	private int current = 0;
	private GLGraphicsOffScreen offscreen;
	private List<PImage> fotos = new ArrayList<PImage>();

	boolean running = false;

	@Override
	public void draw(Thing cosa, PApplet pApplet,
			GLGraphicsOffScreen offscreen, Object object) {

		offscreen.background(0);

		if (currentPhoto != null) {
			offscreen.image(currentPhoto, 0, 0);
		}
	}

	private void updateNextPhoto() {
		if (nextPhoto != null && nextPhoto.width > 0) {
			currentPhoto = nextPhoto;
			nextPhoto = null;
		}
	}

	public void draw(Thing cosa, PApplet pApplet, Object object) {
		updateNextPhoto();
	}

	public void draw(Thing body, PApplet pApplet) {
		draw(body, pApplet, null);
	}

	public void turnOn() {

		if (files != null) {
			current++;
			if (current >= fotos.size()) {
				current = 0;
			}
			currentPhoto = fotos.get(current);
		}
	}

	@Override
	public void setConf(ActionConf actionConf) {
	}

	@Override
	public String getName() {
		return "Photo Viewer";
	}

	@Override
	public EventPainter controlEvent(GUIEvent theEvent) {
		EventPainter eventPainter = utilPainter.controlEvent(theEvent);
		if (eventPainter != null)
			setParameters(eventPainter.name, eventPainter.value);
		return eventPainter;
	}

	private void openPath() {
		fotos.clear();
		photoPath = pApplet.selectFolder("Only  .gif, .jpg, .tga, .png");
		if (photoPath != null) {
			File path = new File(photoPath);
			if (path.isDirectory()) {
				files = path.listFiles();
				for (int index = 0; index < files.length; index++) {
					PImage image = pApplet.loadImage(files[index]
							.getAbsolutePath());
					if (image != null) {
						image.resize(offscreen.width, offscreen.height);
						fotos.add(image);
					}
				}
				current = 0;
			}
		}
	}

	private void setParameters(String name, float value) {

		if (name.equals("Open Photo Path")) {
			openPath();
		}
	}

	@Override
	public void showForm(int x, int y) {
		utilPainter.setPosition(x, y);
		utilPainter.render();
	}

	@Override
	public void hideForm() {
		utilPainter.hide();
	}

	public ActionConf createNewActionConf() {
		return utilPainter.createActionConf();
	}

	@Override
	public ThingPainterDelegate createNewInstance() {
		FotoViewer fotoViewer = new FotoViewer();
		fotoViewer.utilPainter = utilPainter.clone();
		fotoViewer.pApplet = pApplet;
		return fotoViewer;
	}

	@Override
	public void setup(PApplet pApplet, GLGraphicsOffScreen offScreen,
			CornerPinSurface surface, ButtonList buttonList,
			GuiFactory guiFactory) {
		this.pApplet = pApplet;
		utilPainter.setup(pApplet, null, buttonList, guiFactory);
		utilPainter.addButton("Open Photo Path");
		this.offscreen = offScreen;
	}

	@Override
	public void setup(PApplet pApplet, ControlFacade controlFacade,
			ButtonList buttonList, GuiFactory guiFactory) {

	}

	@Override
	public void stop(World world, PApplet pApplet, ControlP5 controlP5) {
		running = false;
	}

	@Override
	public void start(World world, PApplet pApplet, ControlP5 controlP5) {
		running = true;
	}

}
